// 全局弹出框样式
$.fn.oemDrawer = function () {
    var valueOfElement = $(this);
    // 生成id
    var id = new Date().getTime() + Math.ceil(Math.random() * 100);

    // 设置id
    $(valueOfElement).find("oem-drawer-label").attr("oem-drawer-id", id);
    $(valueOfElement).find("oem-drawer-close").attr("oem-drawer-id", id);
    $(valueOfElement).find("oem-drawer-popup-mask").attr("oem-drawer-id", id);

    // 获取元素弹出框元素追加在body 删除元素
    var drawerPopup = $(valueOfElement).find("oem-drawer-popup");
    drawerPopup.attr("oem-drawer-id", id);
    $("body").append(drawerPopup);
    $(valueOfElement).find("oem-drawer-popup").remove();

    //显示元素
    var drawerLabel = $("oem-drawer-label");
    drawerLabel.click(function () {
        var id = $(this).attr("oem-drawer-id");
        var cuurentDrawerPopup = $("oem-drawer-popup[oem-drawer-id=" + id + "]");
        cuurentDrawerPopup.addClass("oem-drawer-open");
        setTimeout(function () {
            cuurentDrawerPopup.find("oem-drawer-popup-warp").addClass("oem-drawer-popup-open");
        }, 100);
    });

    // 删除元素
    var drawerClose = $("oem-drawer-close");
    drawerClose.click(function () {
        var id = $(this).attr("oem-drawer-id");
        var cuurentDrawerPopup = $("oem-drawer-popup[oem-drawer-id=" + id + "]");
        cuurentDrawerPopup.find("oem-drawer-popup-warp").removeClass("oem-drawer-popup-open");
        setTimeout(function () {
            cuurentDrawerPopup.removeClass("oem-drawer-open");
        }, 350);
    });
};

//1、页面初始化、 2、积木块变更

var COMPONENT_OBJECT = {};

$.extend({
    componentInject: function (componentName, callback) {
        if (!COMPONENT_OBJECT[componentName]) {
            COMPONENT_OBJECT[componentName] = callback;
        }
    },
    componentHandle: function (componentName, params) {
        setTimeout(function () {
            COMPONENT_OBJECT[componentName] && COMPONENT_OBJECT[componentName](params);
        }, 10);
    }
});

document.addEventListener("DOMContentLoaded", function () {
    function currencyInit() {
        $(document).click(function () {
            $("oem-currency").removeClass("drop_currency_on");
            $(".header_box_wrap .search_form_box").addClass("hidden");
        });

        $("oem-currency oem-currency-label").on("click", function (e) {
            e.stopPropagation();
            $(this).parent().toggleClass("drop_currency_on");
        });

        $("oem-currency drop_currency").on("click", function (e) {
            e.stopPropagation();
        });

        $("oem-currency web-currency").on("click", function (e) {
            e.stopPropagation();
            currencyPath($(this).data("type"));
        });

        function currencyPath(val) {
            const currency = moi.getUrlParam("currency");
            if (currency || location.href.indexOf("theme_id") > -1) {
                location.href = moi.changeURLArg(location.href, "currency", val);
            } else {
                location.href = moi.addURLParam(location.href, "currency", val);
            }
        }
    }
    currencyInit();

    $(".mobile_currency-select").val("{{currency.standard_code}}");
    $(".mobile_currency-select").change(function (params) {
        currencyPath($(this).val());
    });
});
